/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package servlet;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Admin
 */
public class newServlet extends HttpServlet {
   
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
   // private static final JspFactory _jspxFactory = JspFactory.getDefaultFactory();


    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
 javax.servlet.http.HttpSession session=request.getSession();
db.dbm dba=new db.dbm();
        try {
 if(session.getAttribute("login").equals(null))
     response.sendRedirect("/nowa/");
            
dba.connect();

String data[][]=dba.zapytaj("select id,saldo from accounts where u_name='"+session.getAttribute("login")+"';");
            //TODO output your page here
String nr=request.getParameter("nr");
if(nr==null){
out.println("<html>");
            out.println("<head>");
            out.println("<title>Bank, strona glowna</title>");
            out.println("</head>");
             out.println("<link rel=\"stylesheet\" type=\"text/css\" href=\"faces/styl.css\"/>");
            out.println("<body>");
             out.println("<h3>Jestes zalogowany jako: "+session.getAttribute("login")+"<a href=\"logout.jsp\"> wyloguj </a></h3>");
             out.println("<table>");
             out.println("<tr><td> Nr. rachunku </td><td>Saldo</td></tr>");
            for(int i=0;i<data.length;i++){
  out.println("<tr>");
           
    out.print("<td><a href=\"/nowa/newServlet?nr="+data[i][0]+"\">"+data[i][0]+"</a></td><td>"+data[i][1]+"</td>");
            out.println("</tr>");
            }
             out.println("</table>");
            //out.println(session.getAttribute("login"));
         //   out.println("<h1>Servlet newServlet at " + request.getContextPath () + "</h1>");

            out.println("</body>");
            out.println("</html>");

}
else doKonto(nr, request,response);
dba.close();
        }catch(NullPointerException f){
             
     response.sendRedirect("/nowa/");
        }
        catch(Exception e) {
              out.println("<link rel=\"stylesheet\" type=\"text/css\" href=\"faces/styl.css\"/>");
           out.println("<h3>Jestes zalogowany jako: "+session.getAttribute("login")+" <a href=\"logout.jsp\">wyloguj </a></h3>");
              out.println("brak otwartych rachunkow<br>");
              out.println("<a href=\"rachunek.jsp\">kliknij</a> aby dodac rachunek");
String sql="select * from podania where u_name='"+session.getAttribute("login")+"';";
              
if(dba.zapytaj(sql).length>0){
                  String[][] tmp=dba.zapytaj(sql);
out.println("<br>Zlozone podania: <br><table><b><tr><td>Imie</td><td>Nazwisko</td><td>Adres</td><td>Limit wyplat</td></tr></b>");
                  for(int i=0;i<tmp.length;i++){
      out.println("<tr>");
                        for(int j=2;j<tmp[0].length;j++)
    out.println("<td>"+tmp[i][j]+"</td>");

      out.println("</tr>");
                  }
out.println("</table>");
              }
            out.close();
        }
    } 
 protected void doKonto(String nr, HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException
 {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        javax.servlet.http.HttpSession session=request.getSession();
        try {
            //String nr=request.getParameter("nr");

            session.setAttribute("nr", nr);
            db.dbm dba=new db.dbm();
            dba.connect();

            String q[][]= dba.zapytaj("select * from accounts where id='"+nr+"';");
            String u[][]= dba.zapytaj("select u_name, u_email from users where u_name='"+q[0][1]+"';");

            out.println("<head>");
             out.println("<link rel=\"stylesheet\" type=\"text/css\" href=\"faces/styl.css\"/>");
            out.println("<title>Bank, strona glowna</title>");
            out.println("</head>");
            out.println("<body>");
             out.println("<h3>Jestes zalogowany jako: "+session.getAttribute("login")+" <a href=\"logout.jsp\">wyloguj</a></h3>");

            //out.println("<a href=\"logout.jsp\">wyloguj</a>");
            out.println("Rachunek nr: "+ q[0][0]);
            out.println("<br>zarejestrowany dla uzytkownika "+q[0][1]+" ");
            out.println("<br>adres email <a href=\"mailto:"+u[0][1]+"\">"+u[0][1]+"</a> ");
            java.util.Date data= new java.util.Date();
            out.println("<br>Stan konta w dniu: "+data.getTime()+" wynosi: "+q[0][2]+" PLN");

session.setAttribute("kasa", q[0][2]);
         out.println("<br><a href=faces/main.jsp>Wyslij przelew</a>");

            out.println("</body>");
            out.println("</html>");
            dba.close();
  } finally { 
            out.close();
        }
        }
    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
